"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 硬件故障类--网卡
Case Name   : 网卡闪断，备机重建，查看是否生成core文件
Create At   : 2020-12-29
Owner       : n@ningyali
Description :
    1、cfe模拟网卡闪断：断网3s，间隔3s，重复10次
    2、 备机重建
    3、查看是否生成core文件
Expect      :
    1、网卡闪断故障注入成功
    2、数据库集群不感知，网卡闪断期间，重建稍有延迟，但结果正常
    3、不会产生core文件
History     :
    2021/1/20 @wan005:适配最新CFE故障打印
"""

import unittest

from yat.test import Node

from testcase.utils.Common import *
from testcase.utils.CommonSH import *


class DMLTempTableAndNetLinkFlash(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby1_sh = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('-----------Opengauss_Reliability_Hardware_Case021:开始执行------------')
        self.primary_root_node = Node(node='PrimaryRoot')
        self.primary_user_node = Node(node='PrimaryDbUser')
        self.com = Common()
        self.Constant = Constant()

    def test_main(self):
        self.log.info("---------------配置core文件路径----------------")
        shell_cmd = '''mkdir /home/core
                        mkdir /home/core_bak
                        chmod 777 /home/core
                        echo "/home/core/core-%e-%u-%s-%h">/proc/sys/kernel/core_pattern
                        ulimit -c unlimited
                        mv /home/core/* /home/core_bak'''
        self.log.info(shell_cmd)
        msg = self.primary_root_node.sh(shell_cmd).result()
        self.log.info(msg)

        self.log.info("---------------注入故障----------------")
        self.log.info("-------------获取网卡---------------")
        shell_cmd = "ifconfig | head -1"
        self.log.info(shell_cmd)
        msg = self.primary_root_node.sh(shell_cmd).result()
        dev_name = msg.split(":")[0].strip()
        self.log.info(msg)

        text = '--step1:模拟网卡闪断：断网3s，间隔3s，重复10次' \
               'expect:网卡闪断故障注入成功--'
        self.log.info(text)
        inject_command = f"rNetLink_flash (repeat,duration,interval,dev) values (3,2,10,{dev_name})"
        msg = self.com.cfe_inject(self.primary_root_node, inject_command)
        self.log.info(msg)
        self.assertIn(self.Constant.cfe_inject_netflash_success_msg, msg,
                      '执行失败:' + text)

        self.log.info("-------------备机重建---------------")
        build_msg = self.standby1_sh.buildStandby()
        self.log.info(build_msg)
        self.assertIn(self.Constant.BUILD_SUCCESS_MSG, build_msg)

        self.log.info("-------------查看集群状态---------------")
        db_status = self.primary_sh.getDbClusterStatus('status')
        self.log.info(db_status)
        self.assertTrue(db_status)

        time.sleep(300)

        self.log.info("-------------查看是否生成core文件---------------")
        shell_cmd = "ls -tl /home/core | head -1"
        self.log.info(shell_cmd)
        msg = self.primary_root_node.sh(shell_cmd).result()
        self.log.info(msg)
        self.assertIn('total 0', msg)

    def tearDown(self):
        self.log.info("-------------查看集群状态---------------")
        db_status = self.primary_sh.getDbClusterStatus('detail')
        self.log.info(db_status)
        self.log.info('--------Opengauss_Reliability_Hardware_Case022:执行完成-----------')
